package utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DbUtil {
	private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
	private static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

	static{
		
	}
	
	public static DataSource getDataSource() {
		return dataSource;
	}
	
	public static Connection getConnect() {
		Connection conn = tl.get();
		if(conn == null) {
			try {
				conn = dataSource.getConnection();
				tl.set(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return conn;
	}
	
	public static QueryRunner queryRunner() {
		return new QueryRunner(dataSource);
	}
}
